package com.goudan.rabbitmq.demo.speed;

import com.goudan.rabbitmq.demo.pojo.PushMsg;
import com.goudan.rabbitmq.demo.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;

import java.util.ArrayList;
import java.util.UUID;

/**
 * @ClassName Test
 * @Description 消息发送性能测试
 * @Author lizejiao
 * @Date 2021/9/7 3:52 下午
 **/
public class SendMsgTest {

    public static final int MESSAGE_COUNT = 100000;

    public static void main(String[] args) throws Exception {
        performanceTest();
    }

   // 发布消息10000条耗时测试: 耗时1873ms
    public static void performanceTest() throws Exception {
        try (Channel channel = RabbitMqUtils.getLocalChannel()) {
            String queueName = "lgd_test";
            channel.queueDeclare(queueName, false, false, false, null);
            long begin = System.currentTimeMillis();
            for (int i = 0; i < MESSAGE_COUNT; i++) {
                String message = createMsg(MESSAGE_COUNT);
                channel.basicPublish("", queueName, null, message.getBytes());
            }
            long end = System.currentTimeMillis();
            System.out.println(" 发布" + MESSAGE_COUNT + " 个消息, 耗时" + (end - begin) + "ms");
        }
    }

    private static String createMsg(int messageCount) {
        PushMsg msg = new PushMsg();
        msg.setTitle("我是标题！！");
        msg.setSummary("我就是摘要.....");
        msg.setUrl("www.baidu.com");
        ArrayList<String> machineIdList = new ArrayList<>();
        for (int j = 0; j < 100; j++) {
            String machineId = UUID.randomUUID().toString();
            machineIdList.add(machineId);
        }
        msg.setMachineId(machineIdList);
        return msg.toJson();
    }
}
